<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    #deatou{border: 1px dashed #eee;width: 100px;height: 100px;background-color: rgba(240, 247, 255, 1);}
    #deatou img{width: 100%;height: 100%;display: none}
    #upload_avatar{color: #0E0EFF;cursor: pointer;}
    .deatou_exp{margin: 10px 0}
    #uploadfile{display: none}
</style>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">
        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>

            <div class="block-first">
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label"><text>*</text>姓名</label>
                            <div class="layui-input-block">
                                <input type="text" name="realname" id="realname" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label"><text>*</text>登录手机号</label>
                            <div class="layui-input-block">
                                <input type="text" name="tel" id="tel" placeholder="请输入" maxlength="11" clearable autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label">登录密码</label>
                            <div class="layui-input-block">
                                <input type="text" name="pass" id="pass" placeholder="请输入，留空则不修改密码" clearable autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label">用户头像</label>
                            <div class="layui-input-block">
                                <div id="deatou">
                                    <input type="hidden" id="avatar" name="avatar">
                                    <input type="hidden" id="avatar_id" name="avatar_id">
                                    <img id="avatar_dea" src="">
                                </div>
                                <div class="deatou_exp">
                                    <input type="file" accept="image/*" id="uploadfile" value="上传头像" />
                                    <span id="upload_avatar">上传头像</span>
                                    <span>（请上传jpg或png文件,文件大小不超过1M）</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label"><text>*</text>所属部门</label>
                            <div class="layui-input-block">
                                <div id="deptid"></div>
                            </div>
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label">用户角色类型</label>
                            <div class="layui-input-block">
                                <div class="radio-block">
                                    <input type="checkbox" name="roletype" lay-filter="roletype" lay-skin="primary" value="1" title="管理单位角色">
                                    <input type="checkbox" name="roletype" lay-filter="roletype" lay-skin="primary" value="2" title="参建单位角色">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item layui-hide" id="roleDiv1">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label">管理单位角色</label>
                            <div class="layui-input-block">
                                <div id="roleid1"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-hide" id="roleDiv2">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md6">
                            <label class="layui-form-label">参建单位角色</label>
                            <div class="layui-input-block">
                                <div id="roleid2"></div>
                            </div>
                        </div>
                        <div class="layui-col-md6">
                            <label class="layui-form-label">参建单位名称</label>
                            <div class="layui-input-block">
                                <input type="text" readonly name="dealid" class="layui-input dealid" onclick="openPage('../public/choose_dealcom.php?page=user&dom=dealid','80%','80%')">
                                <div class="choose_div" id="dealid" key="1"></div>
                                <span class="choose_one" onclick="openPage('../public/choose_dealcom.php?page=user&dom=dealid','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                            </div>
                        </div>
                    </div>
                </div>




                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label">账号状态</label>
                            <div class="layui-input-block">
                                <div class="radio-block" id="statusDiv">
                                    <input type="radio" name="status" lay-filter="status" checked value="1" title="正常">
                                    <input type="radio" name="status" lay-filter="status" value="0" title="停用">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item height_auto">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

            <?php include '../public/upinfo.php'?>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    $("#commentForm").validate({
        rules: {
            realname: {required: true},
            tel: {required: true},
        },
        messages:{
            realname: {required: "请输入"},
            tel: {required: "请输入"},
        }
    });

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();
        facade.ajax({type: 'get', url: ask_user_edit, data:{id: param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $("#realname").val(data.realname);
                $("#tel").val(data.tel);
                $("#explain").val(data.explain);


                if(data.avatar){
                    $("#avatar_id").val(data.avatar_id);
                    $("#avatar").val(data.avatar);
                    $("#avatar_dea").attr('src',get_file_complete(data.avatar)).show();
                }



                var status_str = '';
                if(data.status == 1){
                    status_str += '<input type="radio" name="status" checked value="1" title="正常">';
                    status_str += '<input type="radio" name="status" value="0" title="停用">';
                }else{
                    status_str += '<input type="radio" name="status" value="1" title="正常">';
                    status_str += '<input type="radio" name="status" checked value="0" title="停用">';
                }
                $('#statusDiv').html(status_str);


                var deptidXmObj = xmselect_dan('deptid', res.data.deptArr, data.deptid, function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#deptid").find('xm-select').removeClass('error');
                    }else{
                        $("#deptid").find('xm-select').addClass('error');
                    }
                });


                xmselect_duo('roleid1', res.data.roleArr[1], data.roleid, function (res) {

                });
                xmselect_duo('roleid2', res.data.roleArr[2], data.roleid, function (res) {

                });


                if(data.dealid_info){
                    set_choose_btn('dealid',data.dealid_info.id,data.dealid_info.name);
                }



                setTimeout(function () {
                    if(data.dealid_info){
                        var h = $("#dealid").height();
                        $(".dealid" ).css('cssText','height:'+ (h+5) +'px !important');
                    }
                },100)


                var roleidArr = data.roleidArr;
                for (var key in roleidArr){
                    var index = parseInt(roleidArr[key])-1
                    $($("input[name='roletype']")[index]).attr('checked', 'checked');
                }
                if(in_array(1, roleidArr)){
                    $("#roleDiv1").removeClass('layui-hide');
                }else{
                    $("#roleDiv1").addClass('layui-hide');
                }
                if(in_array(2, roleidArr)){
                    $("#roleDiv2").removeClass('layui-hide');
                }else{
                    $("#roleDiv2").addClass('layui-hide');
                }

                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });


        $("#upload_avatar").click(function () {
            $("#uploadfile").click();
        });

        $("#uploadfile").change(function () {
            var loading = layui.layer.load(0, {shade: 0.1});
            let fd = new FormData();
            fd.append('file', this.files[0]);
            fd.append('token', facade.getCookie('token'));
            $.ajax({
                url: server_api_name+ask_upload_avatar,
                type: "POST",
                data: fd,
                contentType: false,
                processData: false,
                dataType: "json",
                success: function (res) {
                    layui.layer.close(loading);
                    if (res.code === 2000) {
                        $("#avatar_id").val(res.data.id);
                        $("#avatar").val(res.data.encpath);
                        $("#avatar_dea").attr('src',get_file_complete(res.data.encpath)).show();
                    }else{
                        facade.error(res.data.msg);
                    }
                },
                error: function (res) {
                    layui.layer.close(loading);
                    facade.error("网络错误");
                },
            })
        });


        form.on('checkbox(roletype)', function(data){
            var arr = get_checkbox_value('roletype');
            if(arr.length < 1){
                $('#roletype_error').show().text('请选择');
                $('#roletype_error').parent().find('input').addClass('select_error');
            }else{
                $('#roletype_error').hide();
                $('#roletype_error').parent().find('input').removeClass('select_error');
            }
            if(in_array(1, arr)){
                $("#roleDiv1").removeClass('layui-hide');
            }else{
                $("#roleDiv1").addClass('layui-hide');
            }
            if(in_array(2, arr)){
                $("#roleDiv2").removeClass('layui-hide');
            }else{
                $("#roleDiv2").addClass('layui-hide');
            }

        });

        form.on('submit(add)', function(data){

            var validRes = $("#commentForm").valid();
            if(!data.field.deptid){
                $("#deptid").find('xm-select').addClass('error');
                validRes = false;
            }else{
                $("#deptid").find('xm-select').removeClass('error');
            }

            var arr = get_checkbox_value('roletype');
            if(arr.length < 1){
                $('#roletype_error').show().text('请选择');
                $('#roletype_error').parent().find('input').addClass('select_error');
            }else{
                $('#roletype_error').hide();
                $('#roletype_error').parent().find('input').removeClass('select_error');
            }

            if(!validRes){return false;}

            var roleid = '0';
            if(in_array(1, arr) && data.field.roleid1){
                roleid += ','+data.field.roleid1
            }
            if(in_array(2, arr) && data.field.roleid2){
                roleid += ','+data.field.roleid2
            }



            data.field.roleid = roleid;
            delete data.field.roletype;
            delete data.field.roleid1;
            delete data.field.roleid2;


            if(in_array(2, arr)){
                data.field.dealid = get_choose_val('dealid');
            }else{
                data.field.dealid = ''
            }





            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            facade.ajax({url: ask_user_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });

    function get_checkbox_value() {
        var obj = document.getElementsByName('roletype');
        var s = [];
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked){
                s.push(parseInt(obj[i].value))
            }
        }
        return s;
    }

</script>
</body>
</html>
